package com.swufe.scoresforapp.Login;

import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.transition.Explode;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.swufe.scoresforapp.DrawerLayoutActivity;
import com.swufe.scoresforapp.R;
import com.swufe.scoresforapp.Yonghu.Dataimport;

public class LoginOneActivity extends AppCompatActivity {

    private EditText etUsername;
    private EditText etPassword;
    private Button btGo;
    private CardView cv;
    private FloatingActionButton fab;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_one);
        initView();
        setListener();
    }

    private void initView() {
        etUsername = findViewById(R.id.et_username);
        etPassword = findViewById(R.id.et_password);
        btGo = findViewById(R.id.bt_go);
        cv = findViewById(R.id.cv);
        fab = findViewById(R.id.fab);
    }
    private void changePage(){
        Explode explode = new Explode();
        explode.setDuration(200);
        getWindow().setExitTransition(explode);
        getWindow().setEnterTransition(explode);


        ActivityOptionsCompat oc2 = ActivityOptionsCompat.makeSceneTransitionAnimation(LoginOneActivity.this);
        Intent i2 = new Intent(LoginOneActivity.this,DrawerLayoutActivity.class);
        SharedPreferences preferences = getSharedPreferences("Data", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("Name",etUsername.getText().toString());
        editor.commit();
        startActivity(i2, oc2.toBundle());
    }//登陆成功后跳转到主界面

    private void setListener() {
        //登陆成功
        btGo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                etUsername = (EditText) findViewById(R.id.et_username);//获取输入的用户名
                etPassword = (EditText) findViewById(R.id.et_password);//获取输入的密码
                handler = new Handler(){
                @Override
                public void handleMessage(Message msg) {
                    if(msg.what == 3){
                        String pass = (String)msg.obj;
                        if(pass.equals("")||pass==null){
                            Toast.makeText(LoginOneActivity.this, "用户不存在", 0).show();
                        }else{//登陆成功
                            if(comparePass(pass)){
                                changePage();
                                Toast.makeText(LoginOneActivity.this, "登陆成功", 0).show();
                            }else{
                                Toast.makeText(LoginOneActivity.this, "密码错误", 0).show();
                            }
                        }
                    }
                }
            };
                String yonghu = etUsername.getText().toString();
                Yonghuexist(yonghu);

            }
        });
        //注册
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getWindow().setExitTransition(null);
                getWindow().setEnterTransition(null);
                ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(LoginOneActivity.this, fab, fab.getTransitionName());
                startActivity(new Intent(LoginOneActivity.this, LoginTwoActivity.class), options.toBundle());

            }
        });
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        fab.setVisibility(View.GONE);
    }

    @Override
    protected void onResume() {
        super.onResume();
        fab.setVisibility(View.VISIBLE);
    }


    //用户信息检验（数据库调用）同时获取密码
    protected void Yonghuexist(final String name){
        new Thread(new Runnable() {
            @Override
            public void run() {
                Dataimport in = new Dataimport();
                String pass = in.getPassword(name);
                Message msg = handler.obtainMessage(3);
                msg.obj = pass;
                handler.sendMessage(msg);
            }
        }).start();
    }

    //密码检验
    protected boolean comparePass(String pass){
        if(pass.equals(etPassword.getText().toString())) return true;
        else return false;
    }
}
